# -*- coding:utf-8 -*-
"""
@Time : 2022/4/24 10:40 AM
@Author: binkuolo
@Des: 基础模型
"""

from tortoise import fields
from tortoise import Model


class AccessLog(Model):
    user_id = fields.IntField(description="用户ID")
    target_url = fields.CharField(null=True, description="访问的url", max_length=255)
    user_agent = fields.CharField(null=True, description="访问UA", max_length=255)
    request_params = fields.JSONField(null=True, description="请求参数get|post")
    ip = fields.CharField(null=True, max_length=32, description="访问IP")
    note = fields.CharField(null=True, max_length=255, description="备注")

    class Meta:
        table_description = "用户操作记录表"
        table = "access_log"


class User(Model):
    username = fields.CharField(null=True, max_length=20, description="用户名")
    user_type = fields.BooleanField(default=False, description="用户类型 True:超级管理员 False:普通管理员")
    password = fields.CharField(null=True, max_length=255)
    bio = fields.TextField(null=True, description="个人简介")
    user_email = fields.CharField(null=True, description='邮箱', max_length=255)
    user_status = fields.IntField(default=1, description='0未激活 1正常 2禁用')
    avatar = fields.CharField(max_length=255, null=True, default='images/avatar/1.jpg', description="头像链接")
    sex = fields.IntField(default=0, null=True, description='0未知 1男 2女')
    client_host = fields.CharField(null=True, max_length=19, description="访问IP")
    level = fields.IntField(null=True, default=1, description="用户等级")
    integral = fields.IntField(null=True, default=10, description="用户积分")
    integral_time = fields.DatetimeField(auto_now_add=True, null=True, description="积分获取时间")
    share_code = fields.CharField(null=True, max_length=255, description="分享码")
    code = fields.CharField(null=True, max_length=255, description="邀请码")
    is_share_code = fields.BooleanField(default=False, description="是否分享过")

    class Meta:
        table_description = "用户表"
        table = "user"


class User_vip(Model):
    id = fields.IntField(pk=True, description="唯一标识符")
    user = fields.ForeignKeyField('models.User', related_name='user_vip', on_delete=fields.CASCADE, description="用户")
    vip_type = fields.IntField(default=0, null=True, description='0钻石会员 1尊贵会员 2至尊会员 ')
    vip_time = fields.CharField(max_length=255, description="会员到期时间", null=True)
    vip_status = fields.BooleanField(default=False, description="是否是会1121员")

    class Meta:
        table = 'user_vip'
class Order(Model):
    id = fields.IntField(pk=True, description="唯一标识符")
    order_price = fields.CharField(max_length=255, description="订单价格")
    order_time = fields.CharField(max_length=255, description="修改时间")
    order_name = fields.CharField(max_length=255, description="名称")
    is_show = fields.BooleanField(default=False, description="是否显示")
    times = fields.IntField(null=True, blank=True, description="到期时间")

    class Meta:
        table = 'order'

class Userposts(Model):
    id = fields.IntField(pk=True, description="唯一标识符")
    user = fields.ForeignKeyField('models.User', related_name='userposts', on_delete=fields.CASCADE, description="用户")
    posts = fields.ForeignKeyField('models.Post', related_name='userposts', on_delete=fields.CASCADE,
                                   description="文章")
    time = fields.CharField(max_length=255, description="购买时间")
    is_order = fields.BooleanField(default=False, description="是否购买成功")
    code = fields.CharField(max_length=255, null=True, blank=True, description="分享码")
    order_link = fields.CharField(max_length=255, null=True, blank=True, description="订单号")
    price = fields.CharField(max_length=255, null=True, blank=True, description="价格")

    class Meta:
        table = 'userposts'


class Viewjilu(Model):
    id = fields.IntField(pk=True)
    user_ip = fields.CharField(max_length=15, description="用户IP")
    create_time = fields.CharField(max_length=255, null=True, blank=True, description="创建时间")
    posts = fields.CharField(max_length=255, null=True, blank=True, description="文章ID")
    users = fields.CharField(max_length=255, null=True, description="最近访问的用户")

    class Meta:
        table = 'viewjilu'


class Tag(Model):
    id = fields.IntField(pk=True, description="唯一标识符")
    name = fields.CharField(max_length=255, description="标签名称")
    description = fields.TextField(null=True, blank=True, description="标签描述")
    tag_img = fields.CharField(max_length=255, null=True, blank=True, description="标签图片")
    is_show = fields.BooleanField(default=True, description="是否显示")
    is_deleted = fields.BooleanField(default=False, description="是否删除")
    price = fields.FloatField(default=9.9, description="价格")

    class Meta:
        table = 'tags'


class Post(Model):
    id = fields.IntField(pk=True, description="唯一标识符")
    imageSrc = fields.TextField(description="图片链接")
    text = fields.TextField(description="文章描述")
    title = fields.CharField(max_length=255, description="文章标题")
    isTop = fields.BooleanField(default=False, description="是否置顶")
    likes = fields.IntField(default=0, description="点赞数量")
    comments_count = fields.IntField(default=0, description="评论数量")
    views = fields.IntField(default=0, description="浏览次数")
    publishDate = fields.CharField(max_length=50, description="发布时间")
    author = fields.ForeignKeyField('models.User', related_name='posts', on_delete=fields.CASCADE, description="作者")
    tags = fields.ForeignKeyField('models.Tag', related_name='posts', null=True, on_delete=fields.CASCADE,
                                  description="标签")
    is_show = fields.BooleanField(default=True, description="是否显示")
    is_slider = fields.BooleanField(default=False, description="是否轮播")
    post_detail = fields.OneToOneField('models.PostDteta', related_name='post', on_delete=fields.CASCADE,
                                       description="文章详细信息")

    class Meta:
        table = 'posts'


class PostDteta(Model):
    id = fields.IntField(pk=True, description="唯一标识符")
    imageSrc = fields.JSONField(description="图片链接")
    Collection = fields.TextField(null=True, blank=True, description="文章合集")
    payType = fields.CharField(max_length=50, null=True, blank=True, description="支付类型，例如 '隐藏', '免费', '会员'")
    level_limit = fields.IntField(default=1, description="阅读等级限制")
    level = fields.CharField(max_length=255, null=True, blank=True, description="文章等级说明")
    data_id = fields.CharField(max_length=20, null=True, blank=True, description="文章等级说明")

    class Meta:
        table = 'postdteta'


class PostDownload(Model):
    id = fields.IntField(pk=True, description="唯一标识符")
    post = fields.ForeignKeyField('models.Post', related_name='downloads', on_delete=fields.CASCADE,
                                  description="对应文章")
    download_url = fields.CharField(max_length=255, description="下载地址")
    tq = fields.CharField(max_length=255, description="网盘密钥")
    jy = fields.CharField(max_length=255, description="解压密钥")
    download_time = fields.CharField(max_length=255, description="保存时间")
    is_show = fields.BooleanField(default=True, description="是否显示")
    is_deleted = fields.BooleanField(default=False, description="是否删除")

    class Meta:
        table = 'post_download'


class Comment(Model):
    id = fields.IntField(pk=True, description="唯一标识符")
    post = fields.ForeignKeyField('models.Post', related_name='comments', on_delete=fields.CASCADE,
                                  description="对应文章")
    user = fields.ForeignKeyField('models.User', related_name='comments', on_delete=fields.CASCADE,
                                  description="评论用户")
    content = fields.TextField(description="评论内容")
    created_at = fields.DatetimeField(auto_now_add=True, null=True, blank=True, description="评论时间")
    is_deleted = fields.BooleanField(default=False, description="是否删除")
    likes = fields.IntField(default=0, description="点赞数量")
    parent = fields.ForeignKeyField('models.Comment', related_name='children', null=True, blank=True,
                                    on_delete=fields.CASCADE, description="父评论")

    class Meta:
        table = 'comments'


class SiteInfo(Model):
    sitemap_url = fields.CharField(max_length=255, null=True, blank=True, description="网站地图链接")
    license_url = fields.CharField(max_length=255, null=True, blank=True, description="备案信息的链接")
    footer_logo_url = fields.CharField(max_length=255, null=True, blank=True, description="页脚logo的链接")
    footer_logo_src = fields.CharField(max_length=255, null=True, blank=True, description="页脚logo图像地址")
    description = fields.TextField(null=True, blank=True, description="关于小镇的描述")
    beiyong = fields.CharField(max_length=255, null=True, blank=True, description="备用字段")

    class Meta:
        table = 'site_info'



